package com.controller;

import java.io.File;


import java.io.IOException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.entity.Clazz;
import com.server.ClazzServer;
import com.server.RecordServer;
import com.server.SysuserServer;
import com.util.PageBean;

@Controller
public class ClazzController {
	@Resource
	private ClazzServer clazzService;
	@Resource
	private SysuserServer userService;
	@Resource
	private RecordServer recordService;


	@RequestMapping("admin/addClazz.do")
	public String addClazz(HttpServletRequest request, Clazz Clazz, HttpSession session) {
		System.out.println("ddddd");
			java.sql.Timestamp time = new Timestamp(System.currentTimeMillis());
			Clazz.setPubtime(time.toString().substring(0, 19));
			Clazz.setIsdel("1");
			clazzService.add(Clazz);
			
			return "redirect:clazzList.do";
	}
// 处理更新分班级
	@RequestMapping("admin/doUpdateClazz.do")
	public String doUpdateClazz(ModelMap map, int id) {
		map.put("jz", clazzService.getById(id));
		
		return "admin/clazz_update";
	}
	
	//查看班级信息
	@RequestMapping("admin/clazz_detail.do")
	public String clazz_detail(ModelMap map, int id) {
		map.put("jz", clazzService.getById(id));
		return "admin/clazz_detail";
	}
// 保存分班级信息
	@RequestMapping("admin/updateClazz.do")
	public String updateClazz(HttpServletRequest request, Clazz clazz) {
		
		clazzService.update(clazz);
		return "redirect:clazzList.do";
	}

	// 分页查询
	@RequestMapping("admin/clazzList.do")
	public String jianList(@RequestParam(value = "page", required = false) String page, ModelMap map,
			HttpSession session) {

		if (page == null || page.equals("")) {
			page = "1";
		}
		PageBean pageBean = new PageBean(Integer.parseInt(page), PageBean.PAGESIZE);
		Map<String, Object> pmap = new HashMap<String, Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", pageBean.getPageSize());
		Map<String, Object> cmap = new HashMap<String, Object>();
		int total = clazzService.getCount(cmap);
		pageBean.setTotal(total);
		List<Clazz> list = clazzService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		map.put("ulist", userService.getAll(null));
		Map<String, Object> umap = new HashMap<>();
		umap.put("utype", "培训师");
		umap.put("status", "空闲中");
		map.put("slist", userService.getAll(umap));
		return "admin/clazz_list";
	}

	// 分页模糊查询
	@RequestMapping("admin/vagueClazzList.do")
	public String vagueClazzList(@RequestParam(value = "page", required = false) String page, ModelMap map,
			HttpSession session, Clazz cd) {
		if (page == null || page.equals("")) {
			page = "1";
		}
		PageBean pageBean = new PageBean(Integer.parseInt(page), PageBean.PAGESIZE);
		Map<String, Object> pmap = new HashMap<String, Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", pageBean.getPageSize());

		Map<String, Object> cmap = new HashMap<String, Object>();
		if (cd.getName() != null && !cd.getName().equals("")) {
			cmap.put("name", cd.getName());
			pmap.put("name", cd.getName());
		}

		int total = clazzService.getCount(cmap);
		pageBean.setTotal(total);
		List<Clazz> list = clazzService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 2);
		Map<String, Object> umap = new HashMap<>();
		umap.put("utype", "培训师");
		umap.put("status", "空闲中");
		map.put("slist", userService.getAll(umap));
		return "admin/clazz_list";
	}

	@RequestMapping("admin/deleteClazz.do")
	public String deleteClazz(int id,HttpSession session) {
		
			clazzService.delete(id);
			return "redirect:clazzList.do";
		

	}
	

	// 文件上传
	public String fileUpload(@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request, String img) {
		String path = request.getSession().getServletContext().getRealPath("upload");
		String fileName = file.getOriginalFilename();
		File targetFile = new File(path, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		try {
			file.transferTo(targetFile);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String pa = request.getContextPath() + "/upload/" + fileName;
		System.out.println("path===" + pa);
		if (fileName != null && !fileName.equals("")) {
			img = fileName;
		} else {
			img = "zanwu.jpg";
		}
		return img;

	}
}
